echo "docker环境下运行Java应用"

app=$1
full_img_ver=$2

if [ -z "$full_img_ver" ]; then
 echo "start-java.sh    应用名   完整的镜像名:版本号"
 echo  "参数: $app,$full_img_ver"
 exit 1;
fi

set -eo pipefail

SCRIPTDIR=$(cd $(dirname "${BASH_SOURCE[0]}") >/dev/null && pwd)
cd $SCRIPTDIR


docker pull $full_img_ver

echo "正在启动 $app ..."

host_ip=$(docker network inspect bridge | grep Gateway | awk '{print $NF}' | sed 's/"//g')

docker kill $app ; docker rm $app

vp=""

if [ "$app" == "mp-admin-api" -o "$app" == "mp-gateway-api" ]; then
   vp=" --privileged=true -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v /etc/docker:/etc/docker  -v /:/r "
fi

if [ "$app" == "mp-admin-api" ]; then
   vp="$vp -m 2048m "
else
   vp="$vp -m 2048m "
fi

if [ "$app" == "mp-gateway-api" ]; then
   vp=" $vp -p 9898:80 "
fi


docker run -d  --name $app --net vm \
  $vp -e PROFILE=dc -e APP_NAME=$app \
  --add-host=dc-app:$host_ip \
  --add-host=amy:172.17.134.92 \
  --add-host=dc-qq:43.143.250.229 \
  --add-host=mysql:172.17.134.92 \
  --add-host=mongo:172.17.134.92 \
  --add-host=redis:172.17.134.92 \
  --add-host=rabbitmq:172.17.134.92 \
  --add-host=es:172.17.134.92 \
  --add-host=minio:172.17.134.92 \
  $full_img_ver